package com.yishusmt.wechatapi.wechatdev.controller;

import com.yishusmt.wechatapi.wechatdev.dto.base.Result;
import com.yishusmt.wechatapi.wechatdev.dto.request.WeChatOrderReq;
import com.yishusmt.wechatapi.wechatdev.entity.WechatMemebrEntity;
import com.yishusmt.wechatapi.wechatdev.service.WeChatOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 订单操作
 *
 * @author zhangyi
 * @date 2018/9/5 16:29
 */
@RestController
@RequestMapping("/order")
@Api(description = "订单相关操作")
public class OrderController extends BaseController{

    @Resource
    private WeChatOrderService weChatOrderService;

    /**
     * 获取订单列表
     *
     * @param status
     * @return
     */
    @GetMapping("/getOrderList/{openId}/{status}")
    @ApiOperation(value = "分类获取订单列表（status: 0,1,2,3,4,500 说明：0未审核 1已通过 2进行中 3已完成 500不通过）",httpMethod = "GET")
    public Result getOrderList(@PathVariable(value = "openId") String openId,
                               @PathVariable(value = "status") String status){
        WechatMemebrEntity member = getMember(openId);
        String[] split = status.split(",");
        List<Integer> statusList = new ArrayList<>();
        List<String> strings = Arrays.asList(split);
        strings.stream().forEach(e -> {
            statusList.add(Integer.parseInt(e));
        });
        Long memberId = (member != null?member.getMemberId():null);
        return weChatOrderService.getOrderList(memberId,statusList);
    }

    /**
     * 获取订单详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getInfo/{id}")
    @ApiOperation(value = "根据订单id获取订单详细信息",httpMethod = "GET")
    public Result getOrderInfo(@PathVariable(value = "id") Long id){
        boolean present = Optional.ofNullable(id).isPresent();
        if(present){
            return weChatOrderService.getOrderInfo(id);
        }else{
           return new Result(null);
        }
    }

    /**
     * 下单
     *
     * @param weChatOrderReq
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "下单",httpMethod = "POST")
    public Result addOrder(@RequestBody WeChatOrderReq weChatOrderReq){

        return weChatOrderService.addOrder(weChatOrderReq);
    }


    /**
     * 订单编辑
     *
     * @param weChatOrderReq
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation(value = "订单编辑",httpMethod = "PUT")
    public Result editOrder(@RequestBody WeChatOrderReq weChatOrderReq){
        return weChatOrderService.editOrder(weChatOrderReq);
    }
}
